﻿using System;
using System.Data;
using System.Collections.Generic;

using Catering_Model;
using System.Collections;
namespace Catering_Rules
{
    /// <summary>
    /// InDbInfo
    /// </summary>
    public partial class InDbInfo
    {
        private readonly Catering_Operation.InDbInfo dal = new Catering_Operation.InDbInfo();
        public InDbInfo()
        { }
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Catering_Model.InDbInfo model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Catering_Model.InDbInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            return dal.Delete(id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            return dal.DeleteList(idlist);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByWhere(string where)
        {
            return dal.DeleteByWhere(where);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Catering_Model.InDbInfo GetModel(int id)
        {

            return dal.GetModel(id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Catering_Model.InDbInfo GetModelByCache(int id)
        {

            string CacheKey = "InDbInfoModel-" + id;
            object objModel = Catering_DBOpertion.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(id);
                    if (objModel != null)
                    {
                        int ModelCache = Catering_DBOpertion.ConfigHelper.GetConfigInt("ModelCache");
                        Catering_DBOpertion.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Catering_Model.InDbInfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.InDbInfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Catering_Model.InDbInfo> DataTableToList(DataTable dt)
        {
            List<Catering_Model.InDbInfo> modelList = new List<Catering_Model.InDbInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Catering_Model.InDbInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Catering_Model.InDbInfo();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["GroupID"] != null && dt.Rows[n]["GroupID"].ToString() != "")
                    {
                        model.GroupID = int.Parse(dt.Rows[n]["GroupID"].ToString());
                    }
                    if (dt.Rows[n]["ClassID"] != null && dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    if (dt.Rows[n]["DBID"] != null && dt.Rows[n]["DBID"].ToString() != "")
                    {
                        model.DBID = int.Parse(dt.Rows[n]["DBID"].ToString());
                    }
                    if (dt.Rows[n]["IntDate"] != null && dt.Rows[n]["IntDate"].ToString() != "")
                    {
                        model.IntDate = DateTime.Parse(dt.Rows[n]["IntDate"].ToString());
                    }
                    if (dt.Rows[n]["PiaoDate"] != null && dt.Rows[n]["PiaoDate"].ToString() != "")
                    {
                        model.PiaoDate = DateTime.Parse(dt.Rows[n]["PiaoDate"].ToString());
                    }
                    if (dt.Rows[n]["RAWID"] != null && dt.Rows[n]["RAWID"].ToString() != "")
                    {
                        model.RAWID = int.Parse(dt.Rows[n]["RAWID"].ToString());
                    }
                    if (dt.Rows[n]["RAW"] != null && dt.Rows[n]["RAW"].ToString() != "")
                    {
                        model.RAW = dt.Rows[n]["RAW"].ToString();
                    }
                    if (dt.Rows[n]["Specifications"] != null && dt.Rows[n]["Specifications"].ToString() != "")
                    {
                        model.Specifications = dt.Rows[n]["Specifications"].ToString();
                    }
                    if (dt.Rows[n]["Number"] != null && dt.Rows[n]["Number"].ToString() != "")
                    {
                        model.Number = dt.Rows[n]["Number"].ToString();
                    }
                    if (dt.Rows[n]["CMP"] != null && dt.Rows[n]["CMP"].ToString() != "")
                    {
                        model.CMP = dt.Rows[n]["CMP"].ToString();
                    }
                    if (dt.Rows[n]["AmountMoney"] != null && dt.Rows[n]["AmountMoney"].ToString() != "")
                    {
                        model.AmountMoney = decimal.Parse(dt.Rows[n]["AmountMoney"].ToString());
                    }
                    if (dt.Rows[n]["Weight"] != null && dt.Rows[n]["Weight"].ToString() != "")
                    {
                        model.Weight = dt.Rows[n]["Weight"].ToString();
                    }
                    if (dt.Rows[n]["RAWTID"] != null && dt.Rows[n]["RAWTID"].ToString() != "")
                    {
                        model.RAWTID = int.Parse(dt.Rows[n]["RAWTID"].ToString());
                    }
                    if (dt.Rows[n]["RAWIDS"] != null && dt.Rows[n]["RAWIDS"].ToString() != "")
                    {
                        model.RAWIDS = int.Parse(dt.Rows[n]["RAWIDS"].ToString());
                    }
                    if (dt.Rows[n]["Back"] != null && dt.Rows[n]["Back"].ToString() != "")
                    {
                        model.Back = dt.Rows[n]["Back"].ToString();
                    }
                    if (dt.Rows[n]["temp1"] != null && dt.Rows[n]["temp1"].ToString() != "")
                    {
                        model.temp1 = dt.Rows[n]["temp1"].ToString();
                    }
                    if (dt.Rows[n]["temp2"] != null && dt.Rows[n]["temp2"].ToString() != "")
                    {
                        model.temp2 = dt.Rows[n]["temp2"].ToString();
                    }
                    if (dt.Rows[n]["Price"] != null)
                    {
                        model.Price = dt.Rows[n]["Price"].ToString();
                    }
                    if (!(dt.Rows[n]["SumNumber"] is DBNull))
                    {
                        model.SumNumber = decimal.Parse(dt.Rows[n]["SumNumber"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 修改结余量
        /// </summary>
        public bool SetJieYu(decimal jieyu, int id)
        {
            return dal.SetJieYu(jieyu, id);
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetList(PageSize, PageIndex, strWhere);
        }

        /// <summary>
        /// 根据条件获取数据总条数
        /// </summary>
        public int GetCountBywhere(string where)
        {
            return dal.GetCountBywhere(where);
        }

        /// <summary>
        /// 根据日期获取台帐
        /// </summary>
        public DataSet GetTZ(string strDatetime, int ClassID, int RawID)
        {
            return dal.GetTZ(strDatetime, ClassID, RawID);
        }

        #endregion  Method
        public DataTable GetInDbInfoMoneyCount(string where)
        {
            return dal.GetInDbInfoMoneyCount(where);
        }

        public ArrayList CanbeInDBInfo(Catering_Model.InDbInfo model)
        {
            ArrayList al = new ArrayList();
            try
            {
                if (model.GroupID < 1)
                {
                    al.Add("NO");
                    al.Add("请选择作业区！");
                    return al;
                }
                if (model.ClassID < 1)
                {
                    al.Add("NO");
                    al.Add("请选择班组！");
                    return al;
                }

                if (Convert.ToDateTime(model.PiaoDate).Year < 1949)
                {
                    al.Add("NO");
                    al.Add("票据日期错误！");
                    return al;
                }

                if (Convert.ToDateTime(model.IntDate).Year < 1949)
                {
                    al.Add("NO");
                    al.Add("添加日期错误！");
                    return al;
                }

                if (Convert.ToDecimal(model.Number) <= 0)
                {
                    al.Add("NO");
                    al.Add("数量必须大于0！");
                    return al;
                }

                if (string.IsNullOrEmpty(model.CMP))
                {
                    al.Add("NO");
                    al.Add("单位错误！");
                    return al;
                }

                if (string.IsNullOrEmpty(model.InDBid))
                {
                    al.Add("NO");
                    al.Add("品号不能为空！");
                    return al;
                }
                else
                {
                    bool flag = false;
                    DataTable dt = new Catering_Rules.RawMaterial_Area().GetList(" InDBid='" + model.InDBid + "' ").Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        int temprawid = Convert.ToInt32(dr["RawID"]);
                        if (temprawid == model.RAWID) flag = true;
                    }
                    dt.Dispose();
                    if (!flag)
                    {
                        al.Add("NO");
                        al.Add("此品号的原料不存在！");
                        return al;
                    }

                }

                if (Convert.ToDecimal(model.Price) <= 0)
                {
                    al.Add("NO");
                    al.Add("价格不能为空！");
                    return al;
                }

                if (model.RAWID <= 0)
                {
                    al.Add("NO");
                    al.Add("原料不存在！");
                    return al;
                }
                else
                {
                    Catering_Model.E_RawMaterial md = new Catering_Rules.T_RawMaterial().GetModel(Convert.ToInt32(model.RAWID));
                    if (md == null)
                    {
                        al.Add("NO");
                        al.Add("原料不存在！");
                        return al;
                    }

                }

                DataTable fdt = new Catering_Rules.InDbInfo().GetList(" ClassID=" + model.ClassID + " AND RAWID=" + model.RAWID + " AND Convert(decimal(18,2),Number)=Convert(decimal(18,2),'" + model.Number + "') AND  InDBid='" + model.InDBid + "' AND Year(PiaoDate)=" + Convert.ToDateTime(model.PiaoDate).Year + " AND Month(PiaoDate)=" + Convert.ToDateTime(model.PiaoDate).Month + " AND Day(PiaoDate)=" + Convert.ToDateTime(model.PiaoDate).Day).Tables[0];
                if (fdt != null && fdt.Rows.Count > 0)
                {
                    al.Add("NO");
                    al.Add("此品号的原料已入库，请勿重复录入！");
                    return al;
                }
            }
            catch
            {
                al.Add("NO");
                al.Add("未知错误！");
                return al;
            }
            al.Add("OK");
            return al;
        }

        #region 入库状态

        public DataTable GetDataStateByInDbInfoNew(string where)
        {
            return dal.GetDataStateByInDbInfoNew(where);
        }

        #endregion

    }
}

